package com.zhl.service.impl;

import com.zhl.dao.UserDao;
import com.zhl.entity.User;
import com.zhl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    /**
     * 根据用户名查询用户
     * @param userName 返回用户信息
     * @return
     */
    @Override
    public User findUserByUserName(String userName) {
        return userDao.findUserByUserName(userName);
    }

    /**
     * 更新用户信息
     * @param user
     */
    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    /**
     * 转账业务
     * @param sendName  发送方
     * @param receiveName 接收方
     * @param money 金额
     */
    @Override
    public void transferUser(String sendName, String receiveName, Double money) {
        User sendUser = userDao.findUserByUserName(sendName);
        User receiveUser = userDao.findUserByUserName(receiveName);

        sendUser.setMoney(sendUser.getMoney()-money);
        receiveUser.setMoney(receiveUser.getMoney()+money);
//        int i = 1/0;
        userDao.updateUser(sendUser);
        userDao.updateUser(receiveUser);
    }
}
